package com.shengruan.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.UserJdbc;
import com.shengruan.admin.jdbc.UserMessageJdbc;
import com.shengruan.admin.vo.User;

public class UserLoginFilter extends BaseAction implements Filter {

	Logger logger = LoggerFactory.getLogger(getClass());

	private UserJdbc userJdbc = (UserJdbc) this.getJdbc(UserJdbc.class);


	public void init(FilterConfig filterConfig) throws ServletException {

	}


	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();

		String servletPath = request.getServletPath();
		if(Constants.USER_LOGOUT.equals(servletPath)) {
			filterChain.doFilter(request, response);
			return;
		}
		User curUser = (User)session.getAttribute(Constants.USER);
		if(curUser==null)
		{
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (Constants.COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {
						String value = cookie.getValue();
						if (StringUtils.isNotBlank(value)) {
							String[] split = value.split("==");
							String userName = split[0];
							String password = split[1];
							try {
								User user = userJdbc.getByUserName(userName);
								if (null != user) {
									if (user.getPassword().equalsIgnoreCase(password)) {
										session.setAttribute(Constants.USER, user);
										request.getSession().setAttribute("autoAuctionState", user.getAutoAuctionState());
										  /***加到在线用户***/
						                OnLineUser.addUser(user);
										/***删除游客****/
						                Visitor visitor = (Visitor) request.getSession().getAttribute("onLineUser");
						                OnlineCounter.removeUser(visitor);
						                request.getSession().setAttribute("onLineUser", null);
						                
						                UserMessageJdbc userMessageJdbc = new UserMessageJdbc();
						                int userMessageCount = userMessageJdbc.getRows(user.getId(), Constants.STATUS_INACTIVE);
						                //System.out.println(Constants.USER_MESSAGE_COUNT+"\t"+userMessageCount);
						                request.getSession().setAttribute(Constants.USER_MESSAGE_COUNT, userMessageCount);
									}
								}
							} catch (Exception e) {
								
							}
						} else {
							
						}
						
					}
				}
			}
		}
		filterChain.doFilter(request, response);
	}

	public void destroy() {

	}
}
